/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var Sender = {
    data: null,

    send: function (url, type, json) {
        jQuery.ajax({
            type: type,
            async: false,
            url: Sender.getUrl(window) + url,
            data: {"params": JSON.stringify(json)},
            contentType: "application/x-www-form-urlencoded",
            dataType: "text",
            cache: false,
            success: function (data) {
                Sender.data = Sender.parseJSON(data);
            },
            error: function (err) {
                alert("您发送的请求没有相应，请与系统管理员联系");
                return err;
            }
        });
        return Sender.data;
    },

    getUrl: function (win, layer) {
        var target = "";
        try {
            if (win) {
                target = win.top.Agency.Target.value;
            } else {
                target = top.Agency.Target.value;
            }
        } catch (e) {
            target = "web";
        }
        if (target && target == "local") {
            if (layer) {
                var strPath = "";
                for (var i = 0; i < layer; i++) {
                    strPath += "../";
                }
                return strPath;
            } else {
                return "../../";
            }
        } else {
            var hrefStr = top.location.href;
            var index = hrefStr.indexOf("\/", 8);
            index = hrefStr.indexOf("\/", index + 1);
            return hrefStr.substring(0, index + 1);//return "http://localhost:8080/spring/";
        }
    },

    parseJSON: function (string) {
        try {
            string = eval('(' + string + ')');
            if(Sender.isString(string)){
                string = eval('(' + string + ')');
            }
            return string;
        } catch (e) {
            throw new SyntaxError("解析JSON字符串出错！");
        }
    },

    getType: function (object) {
        var _t;
        return ((_t = typeof(object)) == "object" ? object == null && "null" || Object.prototype.toString.call(object).slice(8, -1) : _t).toLowerCase();
    },

    isString: function(obj){
        return Sender.getType(obj) == "string";
    }
};